﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EM.Components.Business;
using EM.Components.Entities;
using System.Security.Principal;
using EM.Web.Code;

namespace EM.Web.Controllers
{
    public class EmailController : Controller
    {
        private EmailProvider _emailService;
        private CampaignProvider _campaignProvider;

        public EmailController() 
        {
            _emailService = new EmailProvider();
            _campaignProvider = new CampaignProvider();
        }

        //
        // GET: /Email/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Email/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Email/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Email/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Email/Edit/5
        /// <summary>
        /// Campaign ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
 
        public ActionResult Edit(int id)
        {
            EM_EmailInstances currentEmail;
            int campaignId = id;
            var campaign = _campaignProvider.Get(campaignId);
            ViewData[WebHelper.ViewDataCampaignName] = campaign.CampaignName;
            currentEmail=campaign.EM_EmailInstances.FirstOrDefault<EM_EmailInstances>();
           // currentEmail = _emailService.GetByCampaignId(campaignId).FirstOrDefault<EM_EmailInstances>();
            return View(currentEmail);
        }

        //
        // POST: /Email/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, EM_EmailInstances email,string button,string Content)
        {
            //back
            if (button == "Back") 
            {
                return RedirectToAction("Edit", "Campaign", new { id=id });
            }
            //Save and Next
            //email.EmailBody = Content;
            try
            {
                if (email.EmailInstanceID < 1)
                {
                    // New email
                    email.CampaignID = id;
                    email.CreatedBy = WindowsIdentity.GetCurrent().Name;
                    email.CreatedDate = DateTime.Now;
                    _emailService.Add(email);
                }
                else 
                {
                    // Update
                    email.UpdatedBy = WindowsIdentity.GetCurrent().Name;
                    email.UpdatedDate = DateTime.Now;
                    _emailService.Update(email);
                }
                return RedirectToAction("Summary", "Campaign", new{ id=id});
            }
            catch
            {
                return View(email);
            }
        }

        //
        // GET: /Email/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Email/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
